<?php

function current_user_can_write_posts($category_id = 0, $page = 1) {
    global $aldb;
    $all = array();
    $own = array();

    $conditions = null;
    $perpage = 20;
    $count = 0;
    $posts->results = array();
    $category_id = intval($category_id);
    $posts->category_id = $category_id;
    $conditions = ' WHERE p.post_type=\'1\' AND p.post_status!=\'2\' ';

    $user = current_user();

    if ($user->is_superuser) {
        if (!empty($category_id)) {
            $conditions .= "AND c.category_id='$category_id'";
        }
    } else {
        $capabilities = get_capability($user->group_id, 'Category');
        if (empty($category_id)) {
            foreach($capabilities as $key => $capability) {
                if (!empty($capability['manage_all'])) {
                    $all[] = $key;
                } else if (!empty($capability['manage_own'])) {
                    $own[] = $key;
                }
            }

            if (empty($all) && empty($own)) {
                return $posts;
            } else {
                if (!empty($all)) {
                    $all_list = implode(',', $all);
                    $conditions .= " AND (c.category_id IN ($all_list) ";
                }
                if (!empty($own)) {
                    $own_list = implode(',', $own);
                    if (!empty($all)) {
                        $conditions .= ' OR ';
                    } else {
                        $conditions .= ' AND ';
                    }
                    $conditions .= "(c.category_id='1' AND p.poster_id='$user->id')";
                }
                if (!empty($all)) {
                    $conditions .= ')';
                }
            }
        } else {
            if (!empty($capabilities[$category_id]['manage_all'])) {
                $conditions = "WHERE c.category_id='$category_id' AND p.post_type='1' AND p.post_status!='2'";
            } else if (!empty($capabilities[$category_id]['manage_own'])) {
                $conditions = "WHERE c.category_id='$category_id' AND p.poster_id='$user->id' AND p.post_type='1' AND p.post_status!='2'";
            }
            if (empty($conditions)) {
                return $posts;
            }
        }
    }
    $count = $aldb->get_var("SELECT COUNT(c.id) FROM $aldb->post2category as c LEFT JOIN $aldb->posts as p ON c.post_id=p.id $conditions");
    $total = ceil($count/$perpage);
    $page = intval($page);
    if (empty($page) || $page < 1) {
        $page = 1;
    } else if ($page > $total) {
        $page = $total;
    }
    $start = ($page -1) * $perpage;
    $posts->page = $page;
    $posts->results = $aldb->get_results("SELECT p.id,p.poster_id,p.post_title,p.post_time,p.star_status,p.post_status, c.category_id FROM $aldb->post2category as c LEFT JOIN $aldb->posts as p ON c.post_id=p.id $conditions ORDER BY p.post_time DESC LIMIT $start, $perpage");
    $posts->pagelink = get_pagelink(Router::get_url() . '/' . ARLICLE_ADMIN . "/posts/posts/$category_id", $page, $count, $perpage,$total);
    return $posts;
}

function get_post_title($id) {
    global $aldb;
    return $aldb->get_var("SELECT post_title FROM $aldb->posts WHERE id='$id' LIMIT 1");
}
?>
